<?php

namespace api\controllers;

use api\service\ShellService;
use teach\util\helper\ResponseHelper;
use Yii;
use yii\filters\Cors;
use yii\rest\ActiveController;
use yii\web\Response;

class ShellController extends ActiveController
{
    public $modelClass = '';

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        $behaviors['corsFilter'] = [
            'class' => Cors::class,
            'cors' => [
                'Origin' => ['*'],
                'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
                'Access-Control-Request-Headers' => ['*'],
                'Access-Control-Allow-Credentials' => true,
                'Access-Control-Max-Age' => 86400,
            ],
        ];
        return $behaviors;
    }

    public function actionGit()
    {
        //TODO 不可用 函数被禁用， 不打算打开
        $output = exec('python /data/wwwroot/default/shell/git.py');
        $jsonData = ResponseHelper::buildJsonData(200, '成功',$output);
        return $this->asJson($jsonData);
    }

    /**
     * 修改用户绑定的mall_id，给商家做流水开通红包支付
     * @return Response
     */
    public function actionMallId()
    {
        $request = Yii::$app->request;
        $mid = $request->get('mid');
        $mid = $mid ? $mid : 10000;
        $map = "mall_id = {$mid}";
        $where = "id = 10014";
        $shellService = new ShellService();
        $res = $shellService->updateMallId($map, $where);
        if($res) {
            $jsonData = ResponseHelper::buildJsonData(200,'成功');
        } else {
            $jsonData = ResponseHelper::buildJsonData(400,'失败');
        }
        return $this->asJson($jsonData);
    }
}